﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System;
using System.Collections.Generic;

namespace Blueprints.Resolution
{
    internal sealed class EnumDiscoverer
    {
        private readonly Dictionary<Type, Dictionary<ulong, string>> valuesByType = new Dictionary<Type, Dictionary<ulong, string>>();
        private readonly Dictionary<Type, Func<object, ulong>> converters = new Dictionary<Type, Func<object, ulong>>();

        public EnumDiscoverer()
        {
            converters = GetConverters();
        }

        public Dictionary<ulong, string> GetValues(Type type)
        {
            Dictionary<ulong, string> enumValues;
            if (valuesByType.TryGetValue(type, out enumValues))
            {
                return enumValues;
            }

            Func<object, ulong> converter = converters[Enum.GetUnderlyingType(type)];
            enumValues = new Dictionary<ulong, string>();
            foreach (object enumValue in Enum.GetValues(type))
            {
                enumValues[converter(enumValue)] = enumValue.ToString();
            }

            valuesByType[type] = enumValues;
            return enumValues;
        }

        public ulong GetValue(Type type, object value)
        {
            return converters[Enum.GetUnderlyingType(type)](value);
        }

        private Dictionary<Type, Func<object, ulong>> GetConverters()
        {
            Dictionary<Type, Func<object, ulong>> converters = new Dictionary<Type, Func<object, ulong>>();
            converters[typeof(sbyte)] = (o) => { return (ulong)(sbyte)o; };
            converters[typeof(short)] = (o) => { return (ulong)(short)o; };
            converters[typeof(int)] = (o) => { return (ulong)(int)o; };
            converters[typeof(long)] = (o) => { return (ulong)(long)o; };
            converters[typeof(byte)] = (o) => { return (byte)o; };
            converters[typeof(ushort)] = (o) => { return (ushort)o; };
            converters[typeof(uint)] = (o) => { return (uint)o; };
            converters[typeof(char)] = (o) => { return (char)o; };
            converters[typeof(ulong)] = (o) => { return (ulong)o; };
            return converters;
        }
    }
}
